<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Thu, 1 Jan 1970 01:00:00 GMT"); // Date in the past
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
ini_set('log_errors', '1');
ini_set('error_log', 'php_error.log');
ini_set('display_errors', '1');

include_once("file.common.php");
autoheader();
authenticate($validCredentials);

$newPassword = req('newPassword');
$newEmail = req('newEmail', $curateEmail);

$newPasscode = $newPassword ? md5($newPassword) : $passcode;
$srcPasscode = $newPassword ? $newPassword : $srcPasscode;

$result = TRUE;
$msg = '';
$log = array();

if ($newPassword || $newEmail)
{
	// Create the passcode file
	$credfile = "<?PHP\n";
	$credfile .= '$passcode = ' . json_encode($newPasscode) . ";\n";
	$credfile .= '$usercode = ' . json_encode($usercode) . ";\n";
	$credfile .= '$curateEmail = ' . json_encode($newEmail) . ";\n";
	$credfile .= '$createTime = ' . json_encode($createTime) . ";\n";
	$credfile .= '$srcPasscode = ' . json_encode($srcPasscode) . ";\n";
	$credfile .= "?>\n";
	file_put_contents("curator.php", $credfile);
}
else
{
	$result = FALSE;
	$msg = "Missing new " . ($newPassword ? "password" : "email");
}

if ($callback)
	$ee = "$callback({\n";
else
	$ee = "{\n";
$ee .= '"result":' . json_encode($result) . ",\n";
//$ee .= '"notactiveId":' . json_encode($notactiveId) . ",\n";
//$ee .= '"password":' . json_encode($password) . ",\n";
//$ee .= '"passcode":' . json_encode($passcode) . ",\n";
$ee .= '"msg":' . json_encode($msg) . ",\n";
$ee .= '"log":' . json_encode($log) . ",\n";
$ee .= '"inactive":' . json_encode($inactive_credentials) . ",\n";
$ee .= '"userid":' . json_encode($usercode) . ",\n";
$ee .= '"email":' . json_encode($curateEmail) . ",\n";
$ee .= '"file":' . json_encode($file) . ",\n";
$ee .= '"path":' . json_encode($path) . ",\n";
$ee .= '"fullPath":' . json_encode($fullPath) . ",\n";
$ee .= '"size":' . json_encode($size) . ",\n";
$ee .= '"lastChanged":' . json_encode($lastChanged) . ",\n";
$ee .= '"host":' . json_encode($_SERVER['SERVER_NAME']) . ",\n";
if ($devserver) {
	$ee .= '"cwd":' . json_encode(getcwd()) . ",\n";
	$ee .= '"root":' . json_encode($_SERVER['DOCUMENT_ROOT']) . ",\n";
}
$ee .= '"end":true' . "\n";
if ($callback)
	$ee .= "})";
else
	$ee .= "}";
echo $ee;
?>
